package com.eduhzy.ttw.clazz.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.clazz.R;
import com.eduhzy.ttw.clazz.R2;
import com.eduhzy.ttw.clazz.di.component.DaggerEditReviewComponent;
import com.eduhzy.ttw.clazz.di.module.EditReviewModule;
import com.eduhzy.ttw.clazz.mvp.contract.EditReviewContract;
import com.eduhzy.ttw.clazz.mvp.model.entity.ClazzScoreData;
import com.eduhzy.ttw.clazz.mvp.model.entity.HomeData;
import com.eduhzy.ttw.clazz.mvp.presenter.EditReviewPresenter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.annotation.ScopeType;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-5 10:49:33
 */
@Route(path = RouterHub.CLAZZ_DELETEREVIEWFRAGMENT)
public class DeleteReviewFragment extends BaseFragment<EditReviewPresenter> implements EditReviewContract.View {
    HomeData mBean;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;

    @Inject
    @ScopeType(2)
    BaseQuickAdapter<ClazzScoreData, AutoBaseViewHolder> mAdapter;
    @Inject
    @ScopeType(2)
    RecyclerView.LayoutManager mLayoutManager;
    @Inject
    RecyclerView.ItemDecoration mDecoration;

    public static DeleteReviewFragment newInstance(HomeData bean, int type) {
        DeleteReviewFragment fragment = new DeleteReviewFragment();
        try {
            HomeData newBean = (HomeData) bean.clone();
            newBean.setType(type);
            fragment.setData(newBean);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerEditReviewComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .editReviewModule(new EditReviewModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.clazz_fragment_delete_review, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        getData();
        mRvList.setLayoutManager(mLayoutManager);
        mRvList.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(() -> getData());
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            ClazzScoreData data = mAdapter.getData().get(position);
            MobclickAgent.onEvent(getActivity(),"ttw_review_delete");
            mPresenter.evaluateTagDelete(position, mBean.getClassId(), data.getTagId());
        });
    }

    private void getData() {
        mPresenter.getEvaluateTagList(mBean.getClassId(), mBean.getType());
    }

    @Override
    public void setData(@Nullable Object data) {
        if (ObjectUtils.isNotEmpty(data)) {
            mBean = (HomeData) data;
        }
    }

    @Override
    public void showLoading() {
        mSwipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeLayout.setRefreshing(false);

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void update(List<ClazzScoreData> data) {
        mAdapter.setNewData(data);
    }
}
